package com.secure.toolkit;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.secure.constant.KeyConstant;
import com.secure.util.RsaUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {
        regex("decryptResponse=false");
        regex("serviceName=sample-service");
        regex("serviceName=sample-service;decryptResponse=false");
        //signatureAndEncrypt2DecryptAndVerifySignature();
        //urlEncodeEncryptData();
    }

    public static void regex(String text){
        //String regex="^\\s*serviceName\\s*=\\s*(?<serviceName>[^;\\s]+){1}(\\s*;\\s*(?<decryptResponse>decryptResponse){1})?\\s*$";
        String regex="^\\s*serviceName\\s*=\\s*(?<serviceName>[^;\\s]+){1}(\\s*;\\s*decryptResponse\\s*=\\s*(?<decryptResponse>(true|false)){1})?\\s*$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        if (matcher.find()) {
            System.out.println("服务名：" + matcher.group("serviceName"));
            System.out.println("加密响应：" + matcher.group("decryptResponse"));
        } else {
            System.out.println("未能匹配到任何内容！");
        }
    }

    //TODO 客户端非body体传参请求需要urlencode
    private static void urlEncodeEncryptData() throws UnsupportedEncodingException {
        String encryptData = "W6KvVtyMMh+mw9dKaqlkIn06byR6b9KMg80ggMXdBOXSQt+KE0Krjt5/CEETp020xagJrOT7vNtAupYSybsM5Lsc7nsD8hVDh5i4zWo6sF2C6oqucxbxDkVAqY/PWEayDGNGpP8znEifo4jFm+njmRqlFXV4OHx4+nyZp+kIvYApv1/Ds+0E5/T+UfH8D/qijPlhl11+355ge8/hwOEyB8AYokauXnGnViIgdRac62E4KX6l4vmhtclYLGlwLMjJ8tUujxNY4RWkF2StsOt1B2UEnO81AZduQ+uFYO3pHP0Z3k3YjkUJvKNwiZenvs1PK0CJcOn+B0GYTjdjYsM7BQ==";
        String encodeData = URLEncoder.encode(encryptData, StandardCharsets.UTF_8.toString());
        System.out.println(encodeData);
    }

    /**
     * 签名加密-->解密验签
     */
    private static void signatureAndEncrypt2DecryptAndVerifySignature() {
        /*
        //TODO 客户端需要加密请求内容，签名请求内容，解密响应内容
         */

        Map<String, Object> plainMap = new HashMap<>();
        plainMap.put("id", 1);
        plainMap.put("name", "小明");

        //密钥对
        KeyPair keyPair = RsaUtil.generateKeyPair();
        String publicKey = KeyConstant.RSA_DEFAULT_PUBLIC_KEY;
        String privateKey = KeyConstant.RSA_DEFAULT_PRIVATE_KEY;
        System.out.println("公钥：" + publicKey);
        System.out.println("私钥：" + privateKey);

        //签名
        String signatureText = RsaUtil.sign(plainMap, privateKey);
        System.out.println("签名：" + signatureText);

        //加密
        plainMap.put("signature", signatureText);
        String encryptText = RsaUtil.encryptByPublicKey(plainMap, publicKey);
        System.out.println("密文：" + encryptText);

        //解密
        Map<String, Object> decryptPlainMap = RsaUtil.decryptByPrivateKey(encryptText, privateKey, new TypeReference<Map<String, Object>>() {
        });
        System.out.println("解密明文：" + decryptPlainMap);
        Object decryptSignatureText = decryptPlainMap.get("signature");
        decryptPlainMap.remove("signature");
        String keyValuePairPlainText = RsaUtil.map2KeyValuePairText(decryptPlainMap);
        boolean verify = RsaUtil.verify(keyValuePairPlainText, decryptSignatureText.toString(), publicKey);
        System.out.println("验签状态：" + (verify ? "成功" : "失败"));
    }

    /**
     * 解密响应
     */
    private static void decryptResponse(){
        String encryptText = "CXGpj6gFkrlpGtGEYTnp4J8KKPvKomXAXEcfjd1ubPybQodso49PA+Kd1s1o/Ia3eG7ekdkWnKb84ezwFiq0TpeteTY1BLHdui+fuJoI6F5+IryS2+KSKiVVCxSmRS7qwASDsWowdvIpx0GX7Z90tdorJxKkramgmoeH/Au/b7k=";
        Map<String, Object> plainMap = RsaUtil.decryptByPublicKey(encryptText, KeyConstant.RSA_DEFAULT_PUBLIC_KEY, Map.class);
        System.out.println(plainMap);
    }

}
